/*
//  Packers_ScheduleViewController.m
//  Packers Schedule
//
//  Created by Brennan Stehling on 10/14/09.
//  Copyright Offwhite 2009. All rights reserved.
*/

#import "Packers_ScheduleViewController.h"
#import "CJSONDeserializer.h"

@implementation Packers_ScheduleViewController

@synthesize table;

/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */

/*
 // Implement loadView to create a view hierarchy programmatically, without using a nib.
 - (void)loadView {
 }
 */

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadCalendarData];
}

- (void) loadCalendarData {
	NSString *wsUrlString = @"http://services.mygroupsapp.com/Data/Calendar?feedUrl=";
	NSString *icsUrlString = [self urlEncode:@"http://www.theresgonnabesnacks.com/data/Packers2009.ics"];
	
	NSString *urlString = [NSString stringWithFormat:@"%@%@", wsUrlString, icsUrlString];
	
	// Create NSURL string from formatted string
	NSURL *url = [NSURL URLWithString:urlString];
	
	// Setup and start async download
	NSURLRequest *request = [[NSURLRequest alloc] initWithURL: url];
	NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
	
	[connection release];
	[request release];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
	// Store incoming data into a string
	NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
	
	NSLog(@"dataString = \"%@\"", dataString);
	
	NSString *jsonString = [NSString stringWithFormat:@"{ \"json\" : %@ }", dataString];
		
	// Create a dictionary from the JSON string
	NSDictionary *dictionary = [self parseString:jsonString];
	
	NSLog(@"dictionary/json = %@", [dictionary objectForKey:@"json"]);
	
	// Read the ICS string with libical
	//NSDictionary *dictionary = [self parseString:icsString];
	
	[dataString release];
}

- (NSDictionary *)parseString:(NSString *) jsonString {
	NSLog(@"jsonString = \"%@\"", jsonString);
	
	// Use TouchJSON to populate the data and dictionary
	NSData *jsonData = [jsonString dataUsingEncoding:NSUTF32BigEndianStringEncoding];
	NSError *error = nil;
	NSDictionary *dictionary = [[CJSONDeserializer deserializer] 
								deserializeAsDictionary:jsonData error:&error];
	
	if (error) {
		NSLog(@"An error occurred while parsing the JSON string: %@", jsonString);
	}
	
	return dictionary;
}

- (NSString *)urlEncode:(NSString *) unescapedString{
	NSString* escapedUrlString = [unescapedString stringByAddingPercentEscapesUsingEncoding:
								  NSASCIIStringEncoding];
	return escapedUrlString;
}

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)dealloc {
	[table release];
    [super dealloc];
}

@end
